package com.attendance.www.hr.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.actionsoft.bpms.api.OpenApiClient;
import com.actionsoft.sdk.service.response.StringResponse;
import com.attendance.www.hr.beans.ApprovalRecordBean;
import com.attendance.www.hr.beans.BMPBean;
import com.attendance.www.hr.beans.RequestBean;
import com.attendance.www.hr.beans.RequestPageBean;
import com.attendance.www.hr.beans.RestVacationBean;
import com.attendance.www.hr.constant.AppConstant;
import com.attendance.www.hr.dao.mapper.ApprovalRecordMapper;
import com.attendance.www.hr.dao.mapper.ApprovalRuleMapper;
import com.attendance.www.hr.dao.mapper.AttendanceMapper;
import com.attendance.www.hr.dao.mapper.ClosedMapper;
import com.attendance.www.hr.dao.mapper.EmployeeMapper;
import com.attendance.www.hr.dao.mapper.EmployeeRoleMapper;
import com.attendance.www.hr.dao.mapper.EmployeeSchedulMapper;
import com.attendance.www.hr.dao.mapper.FurloughBalanceMapper;
import com.attendance.www.hr.dao.mapper.FurloughMapper;
import com.attendance.www.hr.dao.mapper.PunchTimeMapper;
import com.attendance.www.hr.dao.mapper.RequestExtMapper;
import com.attendance.www.hr.dao.mapper.RequestMapper;
import com.attendance.www.hr.dao.mapper.RetroactiveMapper;
import com.attendance.www.hr.dao.mapper.RoleMapper;
import com.attendance.www.hr.dao.mapper.SchedulDayMapper;
import com.attendance.www.hr.dao.mapper.SystemConfigureMapper;
import com.attendance.www.hr.entitys.ApprovalRecord;
import com.attendance.www.hr.entitys.ApprovalRecordExample;
import com.attendance.www.hr.entitys.ApprovalRule;
import com.attendance.www.hr.entitys.ApprovalRuleExample;
import com.attendance.www.hr.entitys.Closed;
import com.attendance.www.hr.entitys.ClosedExample;
import com.attendance.www.hr.entitys.Employee;
import com.attendance.www.hr.entitys.EmployeeExample;
import com.attendance.www.hr.entitys.EmployeeRole;
import com.attendance.www.hr.entitys.EmployeeRoleExample;
import com.attendance.www.hr.entitys.EmployeeSchedul;
import com.attendance.www.hr.entitys.EmployeeSchedulExample;
import com.attendance.www.hr.entitys.Furlough;
import com.attendance.www.hr.entitys.FurloughExample;
import com.attendance.www.hr.entitys.PunchTime;
import com.attendance.www.hr.entitys.Request;
import com.attendance.www.hr.entitys.RequestExample;
import com.attendance.www.hr.entitys.RequestExample.Criteria;
import com.attendance.www.hr.entitys.Role;
import com.attendance.www.hr.entitys.RoleExample;
import com.attendance.www.hr.exception.ResultFaultException;
import com.attendance.www.hr.service.IApprovalService;
import com.attendance.www.hr.service.IBpmService;
import com.attendance.www.hr.service.IRequestService;
import com.attendance.www.hr.service.IRestVacationService;
import com.attendance.www.hr.utils.JsonDateValueProcessor;
import com.attendance.www.hr.utils.TimeUtils;

import jcifs.dcerpc.msrpc.srvsvc;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

/**
 * 申请service
 * @author zx
 *
 */
@Service
public class RequestServiceImpl implements IRequestService {

	@Resource
	private RequestMapper requestDao;
	@Resource
	private RequestExtMapper requestExtDao;
	@Resource
	private SystemConfigureMapper systemConfigureDao;
	@Resource
	private ApprovalRecordMapper approvalRecordDao;
	@Resource
	private EmployeeRoleMapper employeeRoleDao;
	@Resource
	private RoleMapper roleDao;
	@Resource
	private ApprovalRuleMapper approvalRuleDao;
	@Resource
	private EmployeeMapper employeeDao;
	@Resource
	private AttendanceMapper attendanceDao;
	@Resource
	private ClosedMapper closedDao;
	@Resource
	private EmployeeSchedulMapper employeeSchedulDao;
	@Resource
	private PunchTimeMapper punchTimeDao;
	@Resource
	private SchedulDayMapper schedulDayDao;
	@Resource
	private RetroactiveMapper retroactiveDao;
	@Resource
	private FurloughBalanceMapper furloughBalanceDao;
	@Resource
	private FurloughMapper furloughDao;
	@Resource
	private IRestVacationService restVacationService;
	@Resource
	private IApprovalService approvalService;
	@Resource
	private IBpmService bpmService;
	
	/**
	 * 保存验证日期区间
	 * 不可用，返回true
	 * @param requestBean
	 * @return
	 */
	@Override
	public boolean verifySaveRequest(RequestBean requestBean){
		List<Furlough> furloughList=furloughDao.selectByExample(new FurloughExample());
		
		//获取申请的日期、时间信息
		Integer employeeId=requestBean.getEmployeeId();
		Date rStartDate=requestBean.getStartDate();
		Date rEndDate=requestBean.getEndDate();
		if (rEndDate==null) {
			rEndDate=rStartDate;
		}
		String rStartTime=requestBean.getStartTime();
		String rStartToTime=requestBean.getStartToTime();
		String rEndTime=requestBean.getEndTime();
		Integer requestId=requestBean.getRequestId();
		Integer rType=requestBean.getRequestType();
		if (rType==7) {
			return false;
		}
		
		//婚假
		if (rType==3) {
			for(int fi=0;fi<furloughList.size();fi++) {
				Furlough furlough=furloughList.get(fi);
				if(furlough.getFurloughType()==rType) {
					if(requestBean.getDayNumber()>furlough.getDayNumber()) {
						//未找到排班信息
						throw new ResultFaultException("4_00024","不能超过"+furlough.getDayNumber()+"天"); 
					}
					break;
				}
			}
		}	
		
		//丧假
		if (rType==4) {
			for(int fi=0;fi<furloughList.size();fi++) {
				Furlough furlough=furloughList.get(fi);
				if(furlough.getFurloughType()==rType) {
					if(requestBean.getDayNumber()>furlough.getDayNumber()) {
						//未找到排班信息
						throw new ResultFaultException("4_00025","不能超过"+furlough.getDayNumber()+"天"); 
					}
					break;
				}
			}
		}
		
		//事假
		if (rType==5) {
			//60分钟
			BigDecimal minute = new BigDecimal(60);
			//8小时
			BigDecimal hour = new BigDecimal(8);
			//小时转换为天数
			Integer dayNumber=requestBean.getHours().divide(minute,0,RoundingMode.HALF_UP).divide(hour,0,RoundingMode.HALF_UP).intValue();
			for(int fi=0;fi<furloughList.size();fi++) {
				Furlough furlough=furloughList.get(fi);
				if(furlough.getFurloughType()==rType) {
					if(dayNumber>furlough.getDayNumber()) {
						//未找到排班信息
						throw new ResultFaultException("4_00026","不能超过"+furlough.getDayNumber()+"天"); 
					}
					break;
				}
			}
		}
		
		if (rType==12) {//产检假
			//取得人员的排班时间
			EmployeeSchedulExample employeeSchedulExample = new EmployeeSchedulExample();
			employeeSchedulExample.or().andEmployeeIdEqualTo(requestBean.getEmployeeId()).andFromDateLessThanOrEqualTo(rStartDate).andThruDateGreaterThanOrEqualTo(rStartDate);
			employeeSchedulExample.or().andEmployeeIdEqualTo(requestBean.getEmployeeId()).andFromDateLessThanOrEqualTo(rStartDate).andThruDateIsNull();
			employeeSchedulExample.setOrderByClause("from_date desc");
			List<EmployeeSchedul> employeeSchedulList = employeeSchedulDao.selectByExample(employeeSchedulExample);
			if(employeeSchedulList==null || employeeSchedulList.size()==0){
				//未找到排班信息
				throw new ResultFaultException("5_00006"); 
			}
			//取得打卡时间配置记录
			PunchTime punchTime = punchTimeDao.selectByPrimaryKey(employeeSchedulList.get(0).getPunchTimeId());
			//打卡时间，from的
			String startFromTime=punchTime.getStartFromTime();
			String endFromTime=punchTime.getEndFromTime();
			if (requestBean.getTimeTnterval()==0) {//上午传0
				rStartTime=startFromTime;
				rEndTime=punchTime.getRestFromTime();
			}
			if (requestBean.getTimeTnterval()==1){//下午传1
				rStartTime=punchTime.getRestToTime();
				rEndTime=endFromTime;
			}
		}
		//申请类型按天、小时分类型比较
		if (rType==3 || rType==4 || rType==11 || rType==13) {//申请类型按天
			//日期，时间交集比较
			//1，开始日期，结束日期
			RequestExample rExample=new RequestExample();
			//申请的日期在别的申请日期当中的，不可
			if (requestId == null) {//新增的情况
				rExample.or().andStartDateLessThanOrEqualTo(rStartDate)
				.andEndDateGreaterThanOrEqualTo(rStartDate)
				.andEmployeeIdEqualTo(employeeId)
				.andApprovalStateNotEqualTo(3);
				
				rExample.or().andStartDateLessThanOrEqualTo(rEndDate)
				.andEndDateGreaterThanOrEqualTo(rEndDate)
				.andEmployeeIdEqualTo(employeeId)
				.andApprovalStateNotEqualTo(3);
				
				rExample.or().andStartDateGreaterThanOrEqualTo(rStartDate)
				.andEndDateLessThanOrEqualTo(rEndDate)
				.andEmployeeIdEqualTo(employeeId)
				.andApprovalStateNotEqualTo(3);
			} else {//修改的情况
				rExample.or().andStartDateLessThanOrEqualTo(rStartDate)
				.andEndDateGreaterThanOrEqualTo(rStartDate)
				.andEmployeeIdEqualTo(employeeId)
				.andApprovalStateNotEqualTo(3)
				.andRequestIdNotEqualTo(requestId);
				
				rExample.or().andStartDateLessThanOrEqualTo(rEndDate)
				.andEndDateGreaterThanOrEqualTo(rEndDate)
				.andEmployeeIdEqualTo(employeeId)
				.andApprovalStateNotEqualTo(3)
				.andRequestIdNotEqualTo(requestId);
				
				rExample.or().andStartDateGreaterThanOrEqualTo(rStartDate)
				.andEndDateLessThanOrEqualTo(rEndDate)
				.andEmployeeIdEqualTo(employeeId)
				.andApprovalStateNotEqualTo(3)
				.andRequestIdNotEqualTo(requestId);
			}
			List<Request> rList=requestDao.selectByExample(rExample);
			if (rList.size()>0) {
				return true;
			} else {
				return false;
			}
			
		} else {//申请类型按小时的
			//日期，时间交集比较
			//1，开始日期，结束日期
			RequestExample rExample1=new RequestExample();
			//申请的日期在别的申请日期当中的，不可
			if (requestId == null) {//新增的情况
				rExample1.or().andStartDateLessThan(rStartDate)
				.andEndDateGreaterThan(rStartDate)
				.andEmployeeIdEqualTo(employeeId)
				.andApprovalStateNotEqualTo(3);
				
				rExample1.or().andStartDateLessThan(rEndDate)
				.andEndDateGreaterThan(rEndDate)
				.andEmployeeIdEqualTo(employeeId)
				.andApprovalStateNotEqualTo(3);
				
				rExample1.or().andStartDateGreaterThan(rStartDate)
				.andEndDateLessThan(rEndDate)
				.andEmployeeIdEqualTo(employeeId)
				.andApprovalStateNotEqualTo(3);
				
				if (rStartDate.compareTo(rEndDate)<0) {//不是跨天，但是申请和记录的两个日期还完全一样的情况
					rExample1.or().andStartDateEqualTo(rStartDate)
					.andEndDateEqualTo(rEndDate)
					.andEmployeeIdEqualTo(employeeId)
					.andApprovalStateNotEqualTo(3);
				}
			} else {//修改的情况
				rExample1.or().andStartDateLessThan(rStartDate)
				.andEndDateGreaterThan(rStartDate)
				.andEmployeeIdEqualTo(employeeId)
				.andApprovalStateNotEqualTo(3)
				.andRequestIdNotEqualTo(requestId);
				
				rExample1.or().andStartDateLessThan(rEndDate)
				.andEndDateGreaterThan(rEndDate)
				.andEmployeeIdEqualTo(employeeId)
				.andApprovalStateNotEqualTo(3)
				.andRequestIdNotEqualTo(requestId);
				
				rExample1.or().andStartDateGreaterThan(rStartDate)
				.andEndDateLessThan(rEndDate)
				.andEmployeeIdEqualTo(employeeId)
				.andApprovalStateNotEqualTo(3)
				.andRequestIdNotEqualTo(requestId);
				
				if (rStartDate.compareTo(rEndDate)<0) {//是跨天，但是申请和记录的两个日期还完全一样的情况
					rExample1.or().andStartDateEqualTo(rStartDate)
					.andEndDateEqualTo(rEndDate)
					.andEmployeeIdEqualTo(employeeId)
					.andApprovalStateNotEqualTo(3)
					.andRequestIdNotEqualTo(requestId);
				}
			}
			
			List<Request> rList1=requestDao.selectByExample(rExample1);
			if (rList1.size()>0) {
				return true;
			}
			
			//申请开始日期和已有的记录日期相等，比较具体时间
			Date startDateDB=null;
			Date endDateDB=null;
			String startTimeDB=null;
			String startToTimeDB=null;
			String endTimeDB=null;
			//取班次的打卡时间
			EmployeeSchedulExample eSExample=new EmployeeSchedulExample();
			eSExample.createCriteria().andEmployeeIdEqualTo(employeeId)
			.andFromDateLessThanOrEqualTo(rStartDate)
			.andThruDateGreaterThanOrEqualTo(rEndDate);
			eSExample.setOrderByClause("from_date desc");
			List<EmployeeSchedul> employeeSchedulList=employeeSchedulDao.selectByExample(eSExample);
			PunchTime punchTime=new PunchTime();
			if (employeeSchedulList.size()>0) {
				EmployeeSchedul employeeSchedul=employeeSchedulList.get(0);
				Integer punchTimeId=employeeSchedul.getPunchTimeId();
				punchTime=punchTimeDao.selectByPrimaryKey(punchTimeId);
			}else{//没有排班信息
				throw new ResultFaultException("5_00006");
			}
			
			//1-尾和记录头相等
			RequestExample rExample11=new RequestExample();
			RequestExample.Criteria requestCriteria11 = rExample11.createCriteria();
			if (requestId != null) {
				requestCriteria11.andRequestIdNotEqualTo(requestId);
			}
			requestCriteria11.andStartDateEqualTo(rEndDate)
			.andEmployeeIdEqualTo(employeeId)
			.andApprovalStateNotEqualTo(3);
			List<Request> rList11=requestDao.selectByExample(rExample11);
			if (rList11.size()>0) {
				for (Request request : rList11) {
					Integer rTypeDB=request.getRequestType();
					if (rTypeDB==3 || rTypeDB==4 || rTypeDB==11 || rTypeDB==13) {
						return true;
					}
					startDateDB=request.getStartDate();
					endDateDB=request.getEndDate();
					startTimeDB=request.getStartTime();
					startToTimeDB=request.getStartToTime();
					endTimeDB=request.getEndTime();
					//取申请的尾（结束日期）的两个时间
					if (rStartDate.compareTo(rEndDate)<0) {//申请跨天
						rStartTime=punchTime.getStartFromTime();
						if (startDateDB.compareTo(endDateDB)<0) {//记录跨天
							if (startToTimeDB!=null) {
								endTimeDB=startToTimeDB;
							} else {
								endTimeDB=punchTime.getEndFromTime();
							}
						}
					}
					boolean isOK=this.verifyTimeUsable(startTimeDB, endTimeDB, rStartTime, rEndTime);
					if (!isOK) {//（判断有点拧劲儿了）
						return true;
					}
				}
			} else {
				//正常
			}
			//2-头和记录尾相等
			RequestExample rExample12=new RequestExample();
			RequestExample.Criteria requestCriteria12 = rExample12.createCriteria();
			if (requestId != null) {
				requestCriteria12.andRequestIdNotEqualTo(requestId);
			}
			requestCriteria12.andEndDateEqualTo(rStartDate)
			.andEmployeeIdEqualTo(employeeId)
			.andApprovalStateNotEqualTo(3);
			List<Request> rList12=requestDao.selectByExample(rExample12);
			if (rList12.size()>0) {
				for (Request request : rList12) {
					Integer rTypeDB=request.getRequestType();
					if (rTypeDB==3 || rTypeDB==4 || rTypeDB==11 || rTypeDB==13) {
						return true;
					}
					startDateDB=request.getStartDate();
					endDateDB=request.getEndDate();
					startTimeDB=request.getStartTime();
					startToTimeDB=request.getStartToTime();
					endTimeDB=request.getEndTime();
					//记录的两个时间
					if (startDateDB.compareTo(endDateDB)<0) {//记录跨天
						startTimeDB=punchTime.getStartFromTime();
					}
					//取申请的尾（结束日期）的两个时间
					if (rStartDate.compareTo(rEndDate)<0) {//申请跨天
						if (rStartToTime!=null) {
							rEndTime=rStartToTime;
						} else {
							rEndTime=punchTime.getEndFromTime();
						}
					}
					boolean isOK=this.verifyTimeUsable(startTimeDB, endTimeDB, rStartTime, rEndTime);
					if (!isOK) {//（判断有点拧劲儿了）
						return true;
					}
				}
			} else {
				//正常
			}
			return false;
		}
	}
	
	/**
	 * 验证时间是否可用，可做申请，参数是两个一日的开始和结束时间
	 * 可用返回true
	 * @return
	 */
	private boolean verifyTimeUsable(String startTimeDB, String endTimeDB, String startTimeRe, String endTimeRe){
		if (TimeUtils.compareTime(startTimeRe, endTimeDB)>=0 || 
				TimeUtils.compareTime(endTimeRe, startTimeDB)<=0) {
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * 修改验证日期区间
	 * @param requestBean
	 * @return
	 */
	@Override
	public boolean verifyUpdateRequest(RequestBean requestBean){
		Request request=requestDao.selectByPrimaryKey(requestBean.getRequestId());
		if(request!=null){
			if(!request.getStartDate().equals(requestBean.getStartDate()) 
					|| !request.getEndDate().equals(requestBean.getEndDate())){
				int count=requestExtDao.getRangeRequestCount(requestBean,request.getRequestId());
				if(count>0){
					return true;
				}
			}
		}
		return false;
	}
	
	/**
	 * 封账验证
	 */
	@Override
	public boolean verifySaveRequestClosed(RequestBean requestBean) {
		//申请的开始日期
		Date startDate=requestBean.getStartDate();
		Calendar cal=Calendar.getInstance();
		cal.setTime(startDate);
		int year=cal.get(Calendar.YEAR);
		int month=cal.get(Calendar.MONTH)+1;	
		// 查询封账的年，月
		ClosedExample closedExample=new ClosedExample();
		closedExample.createCriteria()
		.andClosedYearEqualTo(year)
		.andClosedMonthEqualTo(month);
		List<Closed> closedList=closedDao.selectByExample(closedExample);
		if(closedList.size()>0){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 新增申请
	 */
	@Override
	public boolean saveRequest(RequestBean requestBean) {
		String interfaceType="hr004";
		Integer employeeId=requestBean.getEmployeeId();
		Date requestStartDate = requestBean.getStartDate();
		Date requestEndDate=requestBean.getEndDate();
		Integer type=requestBean.getRequestType();
		//申请的开始时间、结束时间
		String requestStartTime=requestBean.getStartTime();
		String requestEndTime=requestBean.getEndTime();
		String startToTime=requestBean.getStartToTime();
		//hours是分钟数
		BigDecimal hours=requestBean.getHours();
		if (type==12) {//产检假
			//取得人员的排班时间
			EmployeeSchedulExample employeeSchedulExample = new EmployeeSchedulExample();
			employeeSchedulExample.or().andEmployeeIdEqualTo(requestBean.getEmployeeId()).andFromDateLessThanOrEqualTo(requestStartDate).andThruDateGreaterThanOrEqualTo(requestStartDate);
			employeeSchedulExample.or().andEmployeeIdEqualTo(requestBean.getEmployeeId()).andFromDateLessThanOrEqualTo(requestStartDate).andThruDateIsNull();
			employeeSchedulExample.setOrderByClause("from_date desc");
			List<EmployeeSchedul> employeeSchedulList = employeeSchedulDao.selectByExample(employeeSchedulExample);
			if(employeeSchedulList==null || employeeSchedulList.size()==0){
				//未找到排班信息
				throw new ResultFaultException("5_00006"); 
			}
			//取得打卡时间配置记录
			PunchTime punchTime = punchTimeDao.selectByPrimaryKey(employeeSchedulList.get(0).getPunchTimeId());
			//打卡时间，from的
			String startFromTime=punchTime.getStartFromTime();
			String endToTime=punchTime.getEndToTime();
			//打卡时间间隔时长
			Integer punchTimeMinutes=0;
			if (requestBean.getTimeTnterval()==0) {//上午传0
				punchTimeMinutes=TimeUtils.differMinutes(startFromTime, punchTime.getRestFromTime());
				requestStartTime=startFromTime;
				requestEndTime=punchTime.getRestFromTime();
			}
			if (requestBean.getTimeTnterval()==1){//下午传1
				punchTimeMinutes=TimeUtils.differMinutes(punchTime.getRestToTime(), endToTime);
				requestStartTime=punchTime.getRestToTime();
				requestEndTime=endToTime;
			}
			hours=new BigDecimal(punchTimeMinutes);
		}
		//小于1小时判断，60分钟
		if (hours!=null && hours.intValue()<60) {
			throw new ResultFaultException("4_00010"); 
		}
		//年假判断，只能审请对应年份的，比较余额是否够用
		if (type==1) {
			if (requestBean.getReportYear()==null) {
				throw new ResultFaultException("0_00001");
			}
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(requestStartDate);
			Integer year = calendar.get(Calendar.YEAR);
	        Integer reportYear=requestBean.getReportYear();
			//查年假余额，减掉使用中的
			RestVacationBean restVacationBean=new RestVacationBean();
			restVacationBean.setFurloughType(1);
			restVacationBean.setStartDate(requestStartDate);
			RestVacationBean newRestVacationBean=restVacationService.getLastYearYearFurlough(restVacationBean, employeeId);
			BigDecimal restThisYearHours=newRestVacationBean.getRestVacation();
			BigDecimal restLastYearHours=newRestVacationBean.getLastYearRestVacation();
			
			if (year.equals(reportYear)) {
				if (hours.compareTo(restThisYearHours)>0) {
					throw new ResultFaultException("4_00002");
				}
			} else {
				if (hours.compareTo(restLastYearHours)>0) {
					throw new ResultFaultException("4_00002");
				}
			}
			Employee emp = employeeDao.selectByPrimaryKey(employeeId);
			//年假开始可以使用日期判断
			Date annDate = emp.getAnnualLeaveDate();
			if (annDate==null) {
				throw new ResultFaultException("4_00023");
			}
			if (requestStartDate.before(annDate)) {
				throw new ResultFaultException("4_00022");
			}
			
		} else if(type==0){//判断调休余额
			RestVacationBean restVacationBean=new RestVacationBean();
			restVacationBean.setFurloughType(0);
			restVacationBean.setStartDate(requestStartDate);
			RestVacationBean newRestVacationBean=restVacationService.getRestVacationByType(restVacationBean, employeeId);
			BigDecimal restHours=newRestVacationBean.getRestVacation();
			if (hours.compareTo(restHours)>0) {
				throw new ResultFaultException("4_00002");
			}
		} else if(type==3){//婚假
			RequestExample requestExample=new RequestExample();
			Date marryDate=requestBean.getMarryDate();
			requestExample.createCriteria().andMarryDateEqualTo(marryDate)
			.andEmployeeIdEqualTo(employeeId)
			.andApprovalStateNotEqualTo(3);
			List<Request> requestList=requestDao.selectByExample(requestExample);
			if (requestList.size()>0) {
				throw new ResultFaultException("4_00016");
			}
		} else if (type==7) {//加班申请
			interfaceType="hr002";
			//出差日不可加班
			RequestExample requestExample=new RequestExample();
			List<Integer> stateList=new ArrayList<>();
			stateList.add(1);
			stateList.add(2);
			Criteria cri=requestExample.createCriteria();
			cri.andRequestTypeEqualTo(8).andStartDateLessThanOrEqualTo(requestStartDate)
			.andEndDateGreaterThanOrEqualTo(requestStartDate)
			.andEmployeeIdEqualTo(employeeId)
			.andApprovalStateIn(stateList);
			cri.andRequestTypeEqualTo(8).andStartDateLessThanOrEqualTo(requestEndDate)
			.andEndDateGreaterThanOrEqualTo(requestEndDate)
			.andEmployeeIdEqualTo(employeeId)
			.andApprovalStateIn(stateList);
			requestExample.or(cri);
			List<Request> requestList=requestDao.selectByExample(requestExample);
			if (requestList.size()>0) {
				throw new ResultFaultException("4_00013");
			}
		}else if(type==9) { //外出
			interfaceType="hr001";
		}else if(type==8) { //出差
			interfaceType="hr005";
		}
		
		Request request = new Request();
		request.setEmployeeId(employeeId);
		request.setRoleId(requestBean.getRoleId());
		request.setRequestDate(new Date());
		request.setRequestType(type);
		request.setStartDate(requestStartDate);
		request.setOvertimeInterval(requestBean.getOvertimeInterval());
		if (type==1) {//年假所用的年假余额的年份
			request.setReportYear(requestBean.getReportYear());
		} else if(type==7) {
			request.setOvertimeType(requestBean.getOvertimeType());
		}
		if (requestEndDate!=null) {//产检假情况判断
			request.setEndDate(requestEndDate);
		} else{
			request.setEndDate(requestStartDate);
		}
		request.setStartTime(requestStartTime);
		request.setEndTime(requestEndTime);
		if (startToTime!=null && startToTime!="") {
			request.setStartToTime(startToTime);
		}
		//申请的是天类型的添加申请天数
		if (type==3||type==4||type==11||type==13) {
			request.setDayNumber(requestBean.getDayNumber());
		} 
		else{//保存申请时长--分钟，再换算成天保存一下
			if (hours==null) {
				throw new ResultFaultException("0_00001");
			}
			request.setHours(hours);
			request.setDayNumber((int) Math.ceil(hours.divide(new BigDecimal("480"), 2, BigDecimal.ROUND_HALF_UP).doubleValue()));
		}
		request.setApprovalState(requestBean.getApprovalState());
		request.setMarryDate(requestBean.getMarryDate());
		
		//判断类型婚假
		if(type==3) {
			//判断是否有结婚登记日
			if(requestBean.getMarryDate()!=null){
				SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
			    String dateString = formatter.format(requestBean.getMarryDate());
				request.setDescription("结婚登记日："+dateString+"，"+requestBean.getDescription());
			}else{
				request.setDescription(requestBean.getDescription());
			}
		}else{
			request.setDescription(requestBean.getDescription());
		}
		
		int record=requestDao.insertSelective(request);
		if(record<=0){
			return false;
		}
		
		//新增审批记录
		if (requestBean.getApprovalState()==1) {//审批状态  1是审批中
			
			/*****屏蔽审批步骤和邮件发送功能*****/
			//取得申请人的岗位级别
			/*Role requestRole = roleDao.selectByPrimaryKey(requestBean.getRoleId());
			// 找到第一条审批岗位数据
			// 如果空岗位的情况，继续找下一级审批岗位
			int nextSeq = 1;
			int nextApprovalRoleLevel = 0;
			Role resultRole = null;
			ApprovalRule approvalRule = getApprovalRule(requestRole,request);
			if(approvalRule == null){
				throw new ResultFaultException("10_00001");
			}
			if (nextSeq == 1) {
				nextApprovalRoleLevel = approvalRule.getOneApprovalRole();
				resultRole = getTempApprovalRoleId(requestRole,nextApprovalRoleLevel,approvalRule.getLowestApprovalRole());
			} 
			if(resultRole==null){
				throw new ResultFaultException("10_00005");
			}*/
			//新增审批记录
			/*ApprovalRecord approvalRecord=new ApprovalRecord();
			approvalRecord.setRequestId(request.getRequestId());
			approvalRecord.setSep(nextSeq);
			approvalRecord.setIsApprove(1);
			approvalRecord.setRoleId(resultRole.getRoleId());
			record=approvalRecordDao.insertSelective(approvalRecord);
			if(record<=0){
				return false;
			}
			//再更新插入审批规则ID  zx
			request.setApprovalRuleId(approvalRule.getApprovalRuleId());
			requestDao.updateByPrimaryKeySelective(request);
			//发起申请后，给审批人发邮件提醒
			this.sendEml(request, employeeId);*/
			/*********************************/
			
			/**
			 * TODO 调用BPM系统发送请求审批接口
			 */
			bpmService.openApplyBPM(request, null,employeeId, interfaceType);
		}
		return true;
	}
	
	
	//TODO 发起申请后，给审批人发邮件提醒
	private void sendEml(Request request,Integer employeeId) {
		List<ApprovalRecordBean> apList = approvalService.getApprovalRecordInfoById(Integer.toString(request.getRequestId()));
		for (ApprovalRecordBean approvalRecordBean : apList) {
			if (approvalRecordBean.getIsApprove()!=null && approvalRecordBean.getIsApprove()==1) {
				Employee emp = employeeDao.selectByPrimaryKey(employeeId);
				String[] strIds=approvalRecordBean.getApproverIds().split(",");
				String textPart = "员工："+ emp.getChineseName() +"的"+approvalRecordBean.getRequestName()+"申请待审批，请及时登陆HR考勤管理系统查看";
				for(int j=0; j<strIds.length; j++){
					approvalService.sendEmail(Integer.parseInt(strIds[j]), textPart, request.getRequestId(), request.getRequestType());
				}
			}
		}
	}
	
	/*
	 * 通过申请人的岗位id，取审批人的岗位
	 * 找不到合适的岗位时，返回null
	 */
	private Role getTempApprovalRoleId(Role requestRole, Integer nextApprovalRoleLevel,Integer lowestApprovalLevel) {
		
		// 取所有父级岗位Id
		String rolePath = requestRole.getRolePath();
		String[] roleIds = reverseArray(rolePath.split("-"));
		List<Integer> roleIdList = new ArrayList<Integer>();
		for (String roleid : roleIds) {
			if(StringUtils.isEmpty(roleid)){
				continue;
			}
			int intRoleId = Integer.parseInt(roleid);
			//跳过岗位本身
			if(intRoleId == requestRole.getRoleId().intValue()){
				continue;
			}
			// 取N+1岗位id
			if (nextApprovalRoleLevel == 99) {
				if(haveEmployeeInRole(intRoleId)){
					Role pareRole = roleDao.selectByPrimaryKey(intRoleId);
					if(lowestApprovalLevel == null || lowestApprovalLevel<= pareRole.getRoleLevel()){
						return pareRole;
					}else{
						continue;
					}
				}else{
					//空岗位跳过
					continue;
				}
			}else{
				roleIdList.add(intRoleId);	
			}
		}
		if (nextApprovalRoleLevel==99 || roleIdList.size()==0) {
			throw new ResultFaultException("10_00005");
		}
		
		// 取指定级别的审批人岗位
		RoleExample roleExample = new RoleExample();
		roleExample.createCriteria().andRoleIdIn(roleIdList);
		roleExample.setOrderByClause("role_level");
		List<Role> roleList = roleDao.selectByExample(roleExample);
		if(roleList==null || roleList.size()==0){
			//没有当前级别的岗位
			return null;
		}
		for (Role role : roleList) {
			//空岗位
			if(haveEmployeeInRole(role.getRoleId())){
				return role;
			}
		}
		return null;
	}
	
	/*
	 * 判断当前岗位是不是空岗位 
	 */
	private boolean haveEmployeeInRole(Integer roleId){
		EmployeeRoleExample example = new EmployeeRoleExample();
		example.or().andRoleIdEqualTo(roleId).andFromDateLessThanOrEqualTo(new Date()).andThruDateIsNull();
		example.or().andRoleIdEqualTo(roleId).andFromDateLessThanOrEqualTo(new Date()).andThruDateGreaterThanOrEqualTo(new Date());

		List<EmployeeRole> employeeRoleList = employeeRoleDao.selectByExample(example);
		if(employeeRoleList==null || employeeRoleList.size()==0){
			return false;
		}
		List<Integer> employeeIds = new ArrayList<Integer>();
		for (EmployeeRole employeeRole : employeeRoleList) {
			employeeIds.add(employeeRole.getEmployeeId());
		}
		EmployeeExample employeeExample = new EmployeeExample();
		EmployeeExample.Criteria employeeCriteria = employeeExample.createCriteria();
		employeeCriteria.andEmployeeIdIn(employeeIds);
		employeeCriteria.andIsDisabledEqualTo(0);
		List<Employee> employeeList = employeeDao.selectByExample(employeeExample);
		if(employeeList==null || employeeList.size()==0){
			return false;
		}
		return true;
	}
	
	private ApprovalRule getApprovalRule(Role requestRole,Request request){
		
		ApprovalRuleExample example = new ApprovalRuleExample();
		example.createCriteria().andRequestTypeEqualTo(request.getRequestType());
		//取得所有审批规则
		List<ApprovalRule> approvalRuleList = approvalRuleDao.selectByExample(example);
		for (ApprovalRule approvalRule : approvalRuleList) {
			if(approvalRule.getRequestRole() == 99 || approvalRule.getRequestRole()==requestRole.getRoleLevel()){
				if(approvalRule.getFromDays() == null){
					if(approvalRule.getToDays() == null){
						return approvalRule;
					}else{
						if(request.getDayNumber()<=approvalRule.getToDays()){
							return approvalRule;
						}
					}
				}else{
					if(approvalRule.getToDays() == null){
						if(request.getDayNumber()>approvalRule.getFromDays()){
							return approvalRule;
						}
					}else{
						if(request.getDayNumber()>approvalRule.getFromDays() && request.getDayNumber()<=approvalRule.getToDays()){
							return approvalRule;
						}
					}
				}
			}
		}
		return null;
	}
	
    /* 
     * 函数：reverseArray  
     * 功能：实现 数组翻转 
     * 例如：{'a','b','c','d'}变成{'d','c','b','a'} 
     */  
    private String[] reverseArray(String[] Array) {  
        String[] new_array = new String[Array.length];  
        for (int i = 0; i < Array.length; i++) {  
            // 反转后数组的第一个元素等于源数组的最后一个元素：  
            new_array[i] = Array[Array.length - i - 1];  
        }  
        return new_array;  
    }  
    
	/**
	 * 查询休假
	 */
	@Override
	public RequestPageBean getFurloughRequestList(RequestBean requestBean, Integer startNum, Integer pageCount) {
		RequestPageBean requestPageBean=new RequestPageBean();
		List<RequestBean> requestBeanList= requestExtDao.getFurloughRequestList(requestBean, startNum,pageCount);
		List<RequestBean> requestBeanList2=new ArrayList<>();
		for (RequestBean requestBean2 : requestBeanList) {
			if (requestBean2.getRequestType()==12) {
				//判断产检是上午、下午
				String startTime=requestBean2.getStartTime();
				String endTime=requestBean2.getEndTime();
				String restTime="12:00";
				if (TimeUtils.compareTime(startTime, restTime)<0) {
					requestBean2.setTimeTnterval(0);
				}
				if (TimeUtils.compareTime(endTime, restTime)>0) {
					requestBean2.setTimeTnterval(1);
				}
			}
			requestBeanList2.add(requestBean2);
		}
		requestPageBean.setRequestBeanList(requestBeanList2);
		int pageTotal=requestExtDao.countByRequestBean(requestBean);
		requestPageBean.setPageTotal(pageTotal);
		return requestPageBean;
	}
	/**
	 * 查询加班
	 */
	@Override
	public RequestPageBean getWorkRequestList(RequestBean requestBean, Integer startNum, Integer pageCount) {
		RequestPageBean requestPageBean=new RequestPageBean();
		List<RequestBean> requestBeanList= requestExtDao.getWorkRequestList(requestBean, startNum,pageCount);
		requestPageBean.setRequestBeanList(requestBeanList);
		int pageTotal=requestExtDao.countByRequestBean(requestBean);
		requestPageBean.setPageTotal(pageTotal);
		return requestPageBean;
	}
	/**
	 * 查询外出和出差
	 */
	@Override
	public RequestPageBean getAwayRequestList(RequestBean requestBean, Integer startNum, Integer pageCount) {
		RequestPageBean requestPageBean=new RequestPageBean();
		List<RequestBean> requestBeanList= requestExtDao.getAwayRequestList(requestBean, startNum,pageCount);
		requestPageBean.setRequestBeanList(requestBeanList);
		int pageTotal=requestExtDao.countByRequestBean(requestBean);
		requestPageBean.setPageTotal(pageTotal);
		return requestPageBean;
	}
	
	/**
	 * 修改申请
	 */
	@Override
	public boolean updateRequest(RequestBean requestBean) {
		String interfaceType="hr004";
		Integer employeeId=requestBean.getEmployeeId();
		Date requestStartDate = requestBean.getStartDate();
		Date requestEndDate=requestBean.getEndDate();
		Integer type=requestBean.getRequestType();
		BigDecimal hours=requestBean.getHours();
		//申请的开始时间  结束时间
		String requestStartTime=requestBean.getStartTime();
		String requestEndTime=requestBean.getEndTime();
		String startToTime=requestBean.getStartToTime();
		
		if (type==12) {//产检假
			//取得人员的排班时间
			EmployeeSchedulExample employeeSchedulExample = new EmployeeSchedulExample();
			employeeSchedulExample.or().andEmployeeIdEqualTo(requestBean.getEmployeeId()).andFromDateLessThanOrEqualTo(requestStartDate).andThruDateGreaterThanOrEqualTo(requestStartDate);
			employeeSchedulExample.or().andEmployeeIdEqualTo(requestBean.getEmployeeId()).andFromDateLessThanOrEqualTo(requestStartDate).andThruDateIsNull();
			employeeSchedulExample.setOrderByClause("from_date desc");
			List<EmployeeSchedul> employeeSchedulList = employeeSchedulDao.selectByExample(employeeSchedulExample);
			if(employeeSchedulList==null || employeeSchedulList.size()==0){
				//未找到排班信息
				throw new ResultFaultException("5_00006"); 
			}
			//取得打卡时间配置记录
			PunchTime punchTime = punchTimeDao.selectByPrimaryKey(employeeSchedulList.get(0).getPunchTimeId());
			//打卡时间，from的
			String startFromTime=punchTime.getStartFromTime();
			String endFromTime=punchTime.getEndToTime(); 
			//打卡时间间隔时长
			Integer punchTimeMinutes=0;
			if (requestBean.getTimeTnterval()==0) {//上午传0
				punchTimeMinutes=TimeUtils.differMinutes(startFromTime, punchTime.getRestFromTime());
				requestStartTime=startFromTime;
				requestEndTime=punchTime.getRestFromTime();
			}
			if (requestBean.getTimeTnterval()==1){//下午传1
				punchTimeMinutes=TimeUtils.differMinutes(punchTime.getRestToTime(), endFromTime);
				requestStartTime=punchTime.getRestToTime();
				requestEndTime=endFromTime;
			}
			hours=new BigDecimal(punchTimeMinutes);
		}
		//小于1小时判断，60分钟
		if (hours!=null && hours.intValue()<60) {
			throw new ResultFaultException("4_00010"); 
		}
		
		//年假判断，只能审请对应年份的，比较余额是否够用 
		if (type==1) {
			if (requestBean.getReportYear()==null) {
				throw new ResultFaultException("0_00001");
			}
			
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(requestStartDate);
			Integer year = calendar.get(Calendar.YEAR);
	        Integer reportYear=requestBean.getReportYear();
			//查年假余额，减掉使用中的
			RestVacationBean restVacationBean=new RestVacationBean();
			restVacationBean.setFurloughType(1);
			restVacationBean.setStartDate(requestStartDate);
			RestVacationBean newRestVacationBean=restVacationService.getLastYearYearFurlough(restVacationBean, employeeId);
			BigDecimal restThisYearHours=newRestVacationBean.getRestVacation();
			BigDecimal restLastYearHours=newRestVacationBean.getLastYearRestVacation();
			
			if (year.equals(reportYear)) {
				if (hours.compareTo(restThisYearHours)>0) {
					throw new ResultFaultException("4_00002");
				}
			} else {
				if (hours.compareTo(restLastYearHours)>0) {
					throw new ResultFaultException("4_00002");
				}
			}
		} else if(type==0){//判断调休余额
			RestVacationBean restVacationBean=new RestVacationBean();
			restVacationBean.setFurloughType(0);
			restVacationBean.setStartDate(requestStartDate);
			RestVacationBean newRestVacationBean=restVacationService.getRestVacationByType(restVacationBean, employeeId);
			BigDecimal restHours=newRestVacationBean.getRestVacation();
			if (hours.compareTo(restHours)>0) {
				throw new ResultFaultException("4_00002");
			}
		} else if(type==3){//婚假
			RequestExample requestExample=new RequestExample();
			Date marryDate=requestBean.getMarryDate();
			requestExample.createCriteria().andMarryDateEqualTo(marryDate)
			.andEmployeeIdEqualTo(employeeId)
			.andApprovalStateNotEqualTo(3)
			.andRequestIdNotEqualTo(requestBean.getRequestId());
			List<Request> requestList=requestDao.selectByExample(requestExample);
			if (requestList.size()>0) {
				throw new ResultFaultException("4_00016");
			}
		} else if (type==7) {//加班申请
			interfaceType="hr002";
			//出差日不可加班
			RequestExample requestExample=new RequestExample();
			List<Integer> stateList=new ArrayList<>();
			stateList.add(1);
			stateList.add(2);
			Criteria cri=requestExample.createCriteria();
			cri.andRequestTypeEqualTo(8).andStartDateLessThanOrEqualTo(requestStartDate)
			.andEndDateGreaterThanOrEqualTo(requestStartDate)
			.andEmployeeIdEqualTo(employeeId)
			.andApprovalStateIn(stateList);
			cri.andRequestTypeEqualTo(8).andStartDateLessThanOrEqualTo(requestEndDate)
			.andEndDateGreaterThanOrEqualTo(requestEndDate)
			.andEmployeeIdEqualTo(employeeId)
			.andApprovalStateIn(stateList);
			requestExample.or(cri);
			List<Request> requestList=requestDao.selectByExample(requestExample);
			if (requestList.size()>0) {
				throw new ResultFaultException("4_00013");
			}
		}else if(type==9) { //外出
			interfaceType="hr001";
		}else if(type==8) { //出差
			interfaceType="hr005";
		}
		
		Request request = new Request();
		request.setRequestId(requestBean.getRequestId());
		request.setEmployeeId(employeeId);
		request.setRoleId(requestBean.getRoleId());
		request.setRequestDate(new Date());
		request.setRequestType(type);
		request.setDescription(requestBean.getDescription());
		request.setStartDate(requestStartDate);
		request.setOvertimeInterval(requestBean.getOvertimeInterval());
		if (type==1) {//年假所用的年假余额的年份
			request.setReportYear(requestBean.getReportYear());
		} else if(type==7) {
			request.setOvertimeType(requestBean.getOvertimeType());
		}
		if (requestEndDate!=null) {//产检假情况判断
			request.setEndDate(requestEndDate);
		} else{
			request.setEndDate(requestStartDate);
		}
		request.setStartTime(requestStartTime);
		request.setEndTime(requestEndTime);
		if (startToTime!=null && startToTime!="") {
			request.setStartToTime(startToTime);
		}
		//申请的是天类型的添加申请天数
		if (type==3||type==4||type==11||type==13) {
			request.setDayNumber(requestBean.getDayNumber());
		} else{//保存申请时长--分钟，再换算成天保存一下
			request.setHours(hours);
			request.setDayNumber((int) Math.ceil(hours.divide(new BigDecimal("480"), 2, BigDecimal.ROUND_HALF_UP).doubleValue()));
		}
		request.setApprovalState(requestBean.getApprovalState());
		request.setMarryDate(requestBean.getMarryDate());
		
		//判断类型婚假
		if(type==3) {
			//判断是否有结婚登记日
			if(requestBean.getMarryDate()!=null){
				SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
				String dateString = formatter.format(requestBean.getMarryDate());
				request.setDescription("结婚登记日："+dateString+"，"+requestBean.getDescription());
			}else{
				request.setDescription(requestBean.getDescription());
			}
		}else{
			request.setDescription(requestBean.getDescription());
		}
		
		int record=requestDao.updateByPrimaryKeySelective(request);
		if(record<=0){
			return false;
		}
		
		//新增审批记录
		if (requestBean.getApprovalState()==1) {//审批状态  1是审批中
			/*//取得申请人的岗位级别
			Role requestRole = roleDao.selectByPrimaryKey(requestBean.getRoleId());
			// 找到第一条审批岗位数据
			// 如果空岗位的情况，继续找下一级审批岗位
			int nextSeq = 1;
			int nextApprovalRoleLevel = 0;
			Role resultRole = null;
			ApprovalRule approvalRule = getApprovalRule(requestRole,request);
			if(approvalRule == null){
				throw new ResultFaultException("10_00001");
			}
			if (nextSeq == 1) {
				nextApprovalRoleLevel = approvalRule.getOneApprovalRole();
				resultRole = getTempApprovalRoleId(requestRole,nextApprovalRoleLevel,approvalRule.getLowestApprovalRole());
				if(resultRole==null){
					nextSeq =2;
				}
			}
			if (nextSeq == 2) {
				if (approvalRule.getTwoApprovalRole()==null) {
					throw new ResultFaultException("10_00005");
				}
				nextApprovalRoleLevel = approvalRule.getTwoApprovalRole();
				resultRole = getTempApprovalRoleId(requestRole,nextApprovalRoleLevel,approvalRule.getLowestApprovalRole());
				if(resultRole==null){
					nextSeq =3;
				}
			} 
			if (nextSeq == 3) {
				if (approvalRule.getThreeApprovalRole()==null) {
					throw new ResultFaultException("10_00005");
				}
				nextApprovalRoleLevel = approvalRule.getThreeApprovalRole();
				resultRole = getTempApprovalRoleId(requestRole,nextApprovalRoleLevel,approvalRule.getLowestApprovalRole());
				if(resultRole==null){
					nextSeq =4;
				}
			}
			if (nextSeq == 4) {
				if (approvalRule.getFourApprovalRole()==null) {
					throw new ResultFaultException("10_00005");
				}
				nextApprovalRoleLevel = approvalRule.getFourApprovalRole();
				resultRole = getTempApprovalRoleId(requestRole,nextApprovalRoleLevel,approvalRule.getLowestApprovalRole());
				if(resultRole==null){
					nextSeq =5;
				}
			} 
			if (nextSeq == 5) {
				if (approvalRule.getFiveApprovalRole()==null) {
					throw new ResultFaultException("10_00005");
				}
				nextApprovalRoleLevel = approvalRule.getFiveApprovalRole();
				resultRole = getTempApprovalRoleId(requestRole,nextApprovalRoleLevel,approvalRule.getLowestApprovalRole());
			}
			if(resultRole==null){
				throw new ResultFaultException("10_00005");
			}
			//新增审批记录
			ApprovalRecord approvalRecord=new ApprovalRecord();
			approvalRecord.setRequestId(request.getRequestId());
			approvalRecord.setSep(nextSeq);
			approvalRecord.setIsApprove(1);
			approvalRecord.setRoleId(resultRole.getRoleId());
			record=approvalRecordDao.insertSelective(approvalRecord);
			if(record<=0){
				return false;
			}
			//再更新插入审批规则ID  zx
			request.setApprovalRuleId(approvalRule.getApprovalRuleId());
			requestDao.updateByPrimaryKeySelective(request);
			//发起申请后，给审批人发邮件提醒
			this.sendEml(request, employeeId);*/
			bpmService.openApplyBPM(request,null, employeeId, interfaceType);
		}
		
		return true;
	}
	
	/**
	 * 删除申请
	 */
	@Override
	public boolean deleteRequest(Integer requestId) {
		int record=requestDao.deleteByPrimaryKey(requestId);
		if(record>0){
			return true;
		}else{
			return false;
		}
	}

	/**
	 * 撤回申请
	 */
	@Override
	public boolean getBackRequest(Integer requestId) {
		// ApprovalState改为0（未提交），删除审批记录（必须是未审批的）
		Request request = requestDao.selectByPrimaryKey(requestId);
		String interfaceType="hr004";
		//类型为加班
		if(request.getRequestType()==7) {
			interfaceType="hr002";
		}
		//类型为出差
		else if(request.getRequestType()==8) {
			interfaceType="hr005";
		}
		//类型为外出
		else if(request.getRequestType()==9) {
			interfaceType="hr001";
		}
		
		Request requestBpm=new Request();
		requestBpm.setEmployeeId(request.getEmployeeId());
		requestBpm.setRequestId(requestId);
		requestBpm.setRequestType(request.getRequestType());
		requestBpm.setRequestDate(request.getRequestDate());
		if(bpmService.revokeBPM(requestBpm, null, request.getEmployeeId(), interfaceType)) {
			request.setApprovalState(0);
			//request.setUpdateTime(null);//设置更新时间
			int record=requestDao.updateByPrimaryKeySelective(request);
			if(record<=0){
				return false;
			}
			//删除审批记录表
			ApprovalRecordExample example=new ApprovalRecordExample();
			example.createCriteria().andRequestIdEqualTo(requestId);
			approvalRecordDao.deleteByExample(example);
		}
		return true;
	}

	/**
	 * 撤回申请验证
	 * true 不能撤回，false 可以撤回
	 */
	@Override
	public boolean verifyGetBackRequest(Integer requestId) {
		ApprovalRecordExample example=new ApprovalRecordExample();
		example.createCriteria().andRequestIdEqualTo(requestId);
		List<ApprovalRecord> arList=approvalRecordDao.selectByExample(example);
		if (arList.size()>1) {
			return true;
		} else if(arList.size() == 1){
			Integer isApprove=arList.get(0).getIsApprove();
			if (isApprove != 1 && isApprove != 4) {//isApprove==4是封账的
				return true;
			}
		}
		return false;
	}
	
}
